package com.gopay.scheduler.job;

import com.gopay.common.core.dic.dao.TransSolutionOrderSnapDao;
import com.gopay.common.domain.solution.TransSolutionOrderSnap;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.AcctProdTransRelSolution;
import com.gopay.scheduler.service.transsolutionordersnap.TransSolutionOrderSnapService;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.*;

/**
 * CLASSNAME:SynchronizationSolutionInfoJob
 * Description:将方案信息缓存到方案排序表中
 * DATE:2018/2/2/002
 * AUTHER:PANSHICHAO
 * www.gopay.com.cn Inc.All rights reserved.
 */
public class SynchronizationSolutionInfoJob extends GopayBaseJob{
    private static final Logger logger = Logger.getLogger(SynchronizationSolutionInfoJob.class);
    @Resource(name = "transSolutionOrderSnapService")
    private TransSolutionOrderSnapService transSolutionOrderSnapService;
    @Resource(name = "solutionOrderSnapDao")
    private TransSolutionOrderSnapDao transSolutionOrderSnapDao;
    @Override
    protected void doJobInternal() {
        logger.error("同步交易方案排序数据定时任务开始。。。。。");
        int targetCount = transSolutionOrderSnapService.findAllTransSolutionOrderSnapCount();
        logger.error("交易方案排序视图表记录数："+targetCount);
        List<AcctProdTransRelSolution> relSolutionList =  transSolutionOrderSnapService.findAllAPTransRelSolution();
        List<AcctProdTransRelSolution> pptaSolutionList = new ArrayList<AcctProdTransRelSolution>();
        List<AcctProdTransRelSolution> ppeaSolutionList = new ArrayList<AcctProdTransRelSolution>();
        for(int i=0;i<relSolutionList.size();i++){
            if(relSolutionList.get(i).getProdCode().equals("PPTA") && relSolutionList.get(i).getProdCodeName().equals("G商融通平台账户")){
               pptaSolutionList.add(relSolutionList.get(i));
            }
            if(relSolutionList.get(i).getProdCode().equals("PPEA") && relSolutionList.get(i).getProdCodeName().equals("G商融通专属账户")){
                ppeaSolutionList.add(relSolutionList.get(i));
            }
        }
        relSolutionList.removeAll(pptaSolutionList);
        relSolutionList.removeAll(ppeaSolutionList);
        int relSolutionListSize = relSolutionList.size();
        logger.error("交易方案记录数："+relSolutionListSize);
        List<TransSolutionOrderSnap> gnstSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> cloudSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> kjrmbzhSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> kjwkzhSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> sqbSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> fdsftSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> fdsrSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> dfsaSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> depoSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> blmeSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> blmkSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> GTPTZHSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> feacSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> kjzhwbSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> gsrtfxjzhSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> KJZHCNYhSolutionList = new ArrayList<TransSolutionOrderSnap>();
        List<TransSolutionOrderSnap> GTZSZHSolutionList = new ArrayList<TransSolutionOrderSnap>();
        if(targetCount == 0){
            logger.error("同步全部交易方案信息到交易方案排序表");
            for(int i =0;i<relSolutionListSize;i++){
                AcctProdTransRelSolution relSolution = relSolutionList.get(i);
                TransSolutionOrderSnap orderSnap = new TransSolutionOrderSnap();
                orderSnap.setOrderByNum(i+1);
                orderSnap.setCreatedTs(new Date());
                orderSnap.setLastUpdTs(new Date());
                orderSnap.setTransCode(relSolution.getIntTxnCd());
                orderSnap.setTransName(relSolution.getIntTxnCdName());
                orderSnap.setProdCode(relSolution.getProdCode());
                orderSnap.setProdCodeName(relSolution.getProdCodeName());
                orderSnap.setTransAuthority(relSolution.getTranPri());
                switch (relSolution.getProdCode()){
                    case "GNST":
                        gnstSolutionList.add(orderSnap);
                        break;
                    case "CLOUD":
                        cloudSolutionList.add(orderSnap);
                        break;
                    case "KJRMBZH":
                        kjrmbzhSolutionList.add(orderSnap);
                        break;
                    case "KJWKZH":
                        kjwkzhSolutionList.add(orderSnap);
                        break;
                    case "SQB":
                        sqbSolutionList.add(orderSnap);
                        break;
                    case "FDSFT":
                        fdsftSolutionList.add(orderSnap);
                        break;
                    case "FDSR":
                        fdsrSolutionList.add(orderSnap);
                        break;
                    case "DFSA":
                        dfsaSolutionList.add(orderSnap);
                        break;
                    case "DEPO":
                        depoSolutionList.add(orderSnap);
                        break;
                    case "BLME":
                        blmeSolutionList.add(orderSnap);
                        break;
                    case "BLMK":
                        blmkSolutionList.add(orderSnap);
                        break;
                    case "GTPTZH":
                        GTPTZHSolutionList.add(orderSnap);
                        break;
                    case "FEAC":
                        feacSolutionList.add(orderSnap);
                        break;
                    case "KJZHWB":
                        kjzhwbSolutionList.add(orderSnap);
                        break;
                    case "GSRTFXJZH":
                        gsrtfxjzhSolutionList.add(orderSnap);
                        break;
                    case "KJZHCNY":
                        KJZHCNYhSolutionList.add(orderSnap);
                        break;
                    case "GTZSZH":
                        GTZSZHSolutionList.add(orderSnap);
                        break;
                }
            }

            if(gnstSolutionList.size() != 0 ){
                for(int i = 0 ; i<gnstSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =gnstSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(cloudSolutionList.size() != 0 ){
                for(int i = 0 ; i<cloudSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = cloudSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(kjrmbzhSolutionList.size() != 0 ){
                for(int i = 0 ; i<kjrmbzhSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = kjrmbzhSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(kjwkzhSolutionList.size() != 0 ){
                for(int i = 0 ; i<kjwkzhSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = kjwkzhSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(sqbSolutionList.size() != 0 ){
                for(int i = 0 ; i<sqbSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =sqbSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(fdsftSolutionList.size() != 0 ){
                for(int i = 0 ; i<fdsftSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = fdsftSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(fdsrSolutionList.size() != 0 ){
                for(int i = 0 ; i<fdsrSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =fdsrSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
             if(dfsaSolutionList.size() != 0 ){
                for(int i = 0 ; i<dfsaSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = dfsaSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(depoSolutionList.size() != 0 ){
                for(int i = 0 ; i<depoSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = depoSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(blmeSolutionList.size() != 0 ){
                for(int i = 0 ; i<blmeSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap = blmeSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(blmkSolutionList.size() != 0 ){
                for(int i = 0 ; i<blmkSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =blmkSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(GTPTZHSolutionList.size() != 0 ){
                for(int i = 0 ; i<GTPTZHSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =GTPTZHSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(feacSolutionList.size() != 0 ){
                for(int i = 0 ; i<feacSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =feacSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(kjzhwbSolutionList.size() != 0 ){
                for(int i = 0 ; i<kjzhwbSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =kjzhwbSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(gsrtfxjzhSolutionList.size() != 0 ){
                for(int i = 0 ; i<gsrtfxjzhSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =gsrtfxjzhSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(KJZHCNYhSolutionList.size() != 0 ){
                for(int i = 0 ; i<KJZHCNYhSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =KJZHCNYhSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
            if(GTZSZHSolutionList.size() != 0 ){
                for(int i = 0 ; i<GTZSZHSolutionList.size();i++){
                    TransSolutionOrderSnap orderSnap =GTZSZHSolutionList.get(i);
                    orderSnap.setOrderByNum(i+1);
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
        }else if(targetCount < relSolutionListSize && targetCount >0){
            List<AcctProdTransRelSolution> transRelSolutionList = new ArrayList<AcctProdTransRelSolution>();
            for (int i =0;i<relSolutionListSize;i++){
                AcctProdTransRelSolution relSolution = relSolutionList.get(i);
                String transCode = relSolution.getIntTxnCd();
                String prodCode = relSolution.getProdCode();
                TransSolutionOrderSnap orderSnap = transSolutionOrderSnapService.getAcctProdTransRelSolutionByTransCodeAndProdCode(transCode,prodCode);
                if(orderSnap == null){
                    transRelSolutionList.add(relSolution);
                }
            }
            int transRelSolutionCount = transRelSolutionList.size();
            logger.info("剩余未同步数据总数为"+transRelSolutionCount);
            if(transRelSolutionCount >0){
                for(int i =0;i<transRelSolutionCount;i++){
                    AcctProdTransRelSolution relSolution = transRelSolutionList.get(i);
                    Integer newOrderByNum = transSolutionOrderSnapService.getLastOrderByNum(relSolution.getProdCode());
                    TransSolutionOrderSnap orderSnap = new TransSolutionOrderSnap();
                    orderSnap.setOrderByNum(newOrderByNum+1);
                    orderSnap.setCreatedTs(new Date());
                    orderSnap.setLastUpdTs(new Date());
                    orderSnap.setTransCode(relSolution.getIntTxnCd());
                    orderSnap.setTransName(relSolution.getIntTxnCdName());
                    orderSnap.setProdCode(relSolution.getProdCode());
                    orderSnap.setProdCodeName(relSolution.getProdCodeName());
                    orderSnap.setTransAuthority(relSolution.getTranPri());
                    transSolutionOrderSnapService.saveTransSolutionOrderSnapInfo(orderSnap);
                }
            }
        }else if(relSolutionListSize == targetCount){
            logger.info("此处为修改方案对应的基础方案开通状态的同步！");
            List<AcctProdTransRelSolution> transRelSolutionList = new ArrayList<AcctProdTransRelSolution>();
            for (AcctProdTransRelSolution relSolution : relSolutionList){
                String transCode = relSolution.getIntTxnCd();
                String prodCode = relSolution.getProdCode();
                String transPri  = relSolution.getTranPri();
                TransSolutionOrderSnap orderSnap = transSolutionOrderSnapService.getTransSolutionOrderSnapInfoByTransCodeAndProdCodeAndTranPriv(transCode,prodCode,transPri);
                if(orderSnap == null){
                    transRelSolutionList.add(relSolution);
                }
            }
            if(transRelSolutionList.size()!=0){
                for(AcctProdTransRelSolution relSolution:transRelSolutionList){
                    String transCode = relSolution.getIntTxnCd();
                    String prodCode = relSolution.getProdCode();
                    TransSolutionOrderSnap orderSnap = transSolutionOrderSnapService.getAcctProdTransRelSolutionByTransCodeAndProdCode(transCode,prodCode);
                    orderSnap.setTransAuthority(relSolution.getTranPri());
                    orderSnap.setLastUpdTs(new Date());
                    transSolutionOrderSnapDao.update(orderSnap);
                }
            }
        }else{
            logger.error("无需同步的数据");
        }
    }
}
